#include "hotblog.h"

HotBlog::HotBlog(QWidget *parent)
{
    vLayout = new QVBoxLayout();
    QLabel *label = new QLabel("热门文章");
    label->setStyleSheet("QLabel{background-color:rgb(250, 0, 102);}");
    vLayout->addWidget(label);
    setLayout(vLayout);
}

void HotBlog::addItem(QString title)
{
   QLabel *label = new QLabel(title);
   labelList << label;
   label->installEventFilter(this);
   vLayout->addWidget(label);
}

// 事件过滤器，实现点击效果
bool HotBlog::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<labelList.length(); i++)
    {
        if(obj == labelList.at(i))
        {
            // 鼠标进入
            if(event->type() == QEvent::Enter)
            {

                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
            }
            // 鼠标离开
            else if (event->type() == QEvent::Leave)
            {

                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(237,243,247);}");

            }
            // 鼠标单击事件
            else if(event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                // 左键单击
                if(mouseEvent->button() == Qt::LeftButton)
                {

                    // 发射信号
                    emit labelClicked(labelList.at(i)->text());
                    return true;

                }
                else
                {
                    return false;
                }
            }
        }
    }

    return QObject::eventFilter(obj, event);
}


HotBlog::~HotBlog()
{

}
